﻿package com.firewolf.zip.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.firewolf.zip.utils.ZipUtil;

/**
 * zip文件工具界面
 * @author Administrator
 *
 */
public class ZipToolUI extends JFrame{
	private final int DEFAULT_WIDTH = 400;  //界面宽度
	private final int DEFAULT_HEIGHT = 300; //界面高度
	private final int LOCATION_X = 200;		//左边界距离
	private final int LOCATION_Y = 200;		//右边界距离
	private JTextArea jTextArea;
	private JMenuBar jMenuBar; // 菜单栏
	private JMenu fileMenu; //菜单：文件
	private JMenuItem pressJMenuItem;//菜单项：压缩
	private JMenuItem unPressJMenuItem;//菜单项：解压
	private JMenuItem exitItem;//菜单项：退出
	public ZipToolUI() {
		setTitle("Zip测试"); //标题
		
		//位置和大小
		setBounds(LOCATION_X,LOCATION_Y,DEFAULT_WIDTH,DEFAULT_HEIGHT);
		
		//菜单
		jMenuBar = new JMenuBar();
		fileMenu = new JMenu("文件");
		pressJMenuItem = new JMenuItem("压缩");
		pressJMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setMultiSelectionEnabled(true);
				chooser.setCurrentDirectory(new File("."));
				int r = chooser.showOpenDialog(ZipToolUI.this);
				if(r == JFileChooser.APPROVE_OPTION){
					File[] files = chooser.getSelectedFiles();
					if(files != null && files.length > 0){
						jTextArea.setText("开始压缩......");
						//压缩
						try {
							ZipUtil.pressFiles(files, files[0].getParent(),files[0].getParent()+"\\zip.zip");
						} catch (FileNotFoundException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}catch(IOException e2){
							e2.printStackTrace();
						}
						jTextArea.setText("压缩完成");
					}else{
						JOptionPane.showMessageDialog(ZipToolUI.this, "请选择文件");
					}
				}
			}	
		});
		
		unPressJMenuItem = new JMenuItem("解压");
		unPressJMenuItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser chooser = new JFileChooser();
				chooser.setCurrentDirectory(new File("."));
				int r = chooser.showOpenDialog(ZipToolUI.this);
				if(r == JFileChooser.APPROVE_OPTION){
					File file = chooser.getSelectedFile();
					if(file != null){
						jTextArea.setText("开始解压......");
						//解压
						try {
							ZipUtil.unpressZipFile(file);
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						}catch(IOException e2){
							e2.printStackTrace();
						}
						jTextArea.setText("解压完成");
					}else{
						JOptionPane.showMessageDialog(ZipToolUI.this, "请选择文件");
					}
					
				}
			}	
		});
		
		exitItem = new JMenuItem("退出");
		exitItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		fileMenu.add(pressJMenuItem);
		fileMenu.add(unPressJMenuItem);
		fileMenu.add(exitItem);
		jMenuBar.add(fileMenu);
		setJMenuBar(jMenuBar);
		jTextArea = new JTextArea();
		add(new JScrollPane(jTextArea),BorderLayout.CENTER);
	}
}